package com.zyasdk.service;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.zyasdk.model.BackDatas;
import com.zyasdk.util.AppInfoUtil;
import com.zyasdk.util.Base64;
import com.zyasdk.util.JSONHelper;
import com.zyasdk.util.SP;
import com.zyasdk.util.StaticSetting;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by briskemen on 2017/10/16.
 */

public class BackDataService extends BaseService{

    public BackDataService(Context context) {
        super(context);
    }

    public void BackData(BackDatas bd){
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        String xx = SP.get(context,"chapingdata");
        if (TextUtils.isEmpty(xx) || xx.equals("-1")) { //随机
            bd.setGetWay("11");
        } else {  // 指定插屏
            bd.setGetWay("10");
        }
        String actionType = SP.get(context,"actiontype");

        params.add(new BasicNameValuePair("packId", bd.getPackId()));
        params.add(new BasicNameValuePair("proId", bd.getProId()));
        params.add(new BasicNameValuePair("handle", bd.getHandle()));
        params.add(new BasicNameValuePair("imei", AppInfoUtil.getIMEI(context)));
        params.add(new BasicNameValuePair("imsi", AppInfoUtil.getIMSI(context)));
        params.add(new BasicNameValuePair("getWay", bd.getGetWay()));
        params.add(new BasicNameValuePair("countryCode", bd.getCountryCode()));
        params.add(new BasicNameValuePair("addTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(System.currentTimeMillis()))));
        params.add(new BasicNameValuePair("getAction", bd.getGetAction()));// 获取类型
        params.add(new BasicNameValuePair("getActionType", actionType));// 每次最多取十条数据


        bd.setImei(AppInfoUtil.getIMEI(context));
        bd.setImsi(AppInfoUtil.getIMSI(context));
        bd.setGetActionType(1+"");
        params.add(new BasicNameValuePair("BackData", new String(Base64.encode(JSONHelper
                .toJSON(bd).getBytes(), Base64.DEFAULT))));
//        ServiceTask.addTask(GetAPIUrl("StatisGeTui"), params);
        Log.i("result", "BackData"+ JSONHelper.toJSON(params));

/*
        Map<String, String> map = new HashMap<String, String>();
        map.put("packId", bd.getPackId());
        map.put("proId", bd.getProId());
        map.put("handle", bd.getHandle());
        map.put("imei", AppInfoUtil.getIMEI(context));
        map.put("imsi", AppInfoUtil.getIMSI(context));
        map.put("getAction", bd.getGetWay());
        map.put("countryCode", bd.getCountryCode());
        map.put("addTime", null);
        map.put("getAction", bd.getGetAction());
        map.put("getActionType", actionType);

        XUtil.Get(StaticSetting.BenDi, map, new Callback.CommonCallback<Object>() {
            @Override
            public void onSuccess(Object o) {
                Log.i("result", "onSuccess ");
            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                Log.i("result", "onError "+b + throwable);
            }

            @Override
            public void onCancelled(CancelledException e) {
                Log.i("result", "onCancelled ");
            }

            @Override
            public void onFinished() {
                Log.i("result", "onFinished ");
            }
        });*/
        ServiceTask.addTask(GetAPIUrl("StatisGeTui"),params);
    }
}
